package wh.iolists;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONException;

import wh.Order;
import wh.ProcessedOrder;
import wh.UnProcessedOrder;

public class UnProcessedOrderList {
	
	private static ArrayList<UnProcessedOrder> upOrder = new ArrayList<UnProcessedOrder>();
	private StringBuilder sb;
	
	public UnProcessedOrderList() {
		
	}
	
	public void addUnprocessedOrder(UnProcessedOrder uOrder)
	{
		upOrder.add(uOrder);
	}
	
	public String getUnProcessedHTML() {
		
		return sb.toString();
	}
	
	public void persistUnProcessedOrdersList()
	{
		try {
			FileOutputStream fos = new FileOutputStream(new File("UnProcessedOrders.html"));
			
			StringBuilder htmlTop = new StringBuilder();
			htmlTop.append("<html><head><title>Un-Processed Orders</title></head>");
			
			sb = new StringBuilder();
			sb.append("<body><table border='1'>");
			sb.append("<tr><th>Order ID ID</th><th>Customer</th><th>Customer ID</th><th>Product ID</th><th>Quantity</th></tr>");
			
			for (UnProcessedOrder po : upOrder)
			{
				sb.append("<tr><th>"+po.getOrderID()+"</th><th>"+po.getCustomerName()+"</th><th>"+po.getCustomerID()+"</th><th>"+
							po.getProductID()+"</th><th>"+po.getQuantity()+"</th></tr>");
			}
			sb.append("</table>");
			htmlTop.append(sb.toString());
			htmlTop.append("</body></html>");
			fos.write(htmlTop.toString().getBytes());
            fos.flush();
			
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}
    
	
	public int getUPOrderListSize()
	{
		return upOrder.size();
	}
}
